package cn.zjj.pet.controller;

import cn.zjj.basic.anno.PreAuthorize;
import cn.zjj.basic.exception.BusinessException;
import cn.zjj.basic.util.JsonResult;
import cn.zjj.basic.util.PageList;
import cn.zjj.pet.domain.SearchMasterMsg;
import cn.zjj.pet.dto.AcceptDto;
import cn.zjj.pet.query.SearchMasterMsgQuery;
import cn.zjj.pet.service.ISearchMasterMsgService;
import cn.zjj.user.domain.Logininfo;
import cn.zjj.user.util.LoginContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
@Api(value = "寻主信息的API",description="寻主信息相关的CRUD功能")
public class SearchMasterMsgController {

    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;

    //接单accept
    @PostMapping("/accept")
    @ApiOperation(value = "接单" )
//    @PreAuthorize(name = "拒单",value = "searchMasterMsg:reject")
    public JsonResult accept(@RequestBody AcceptDto adto){
        try {
            searchMasterMsgService.accept(adto);
            return JsonResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"操作失败");
        }
    }






    //拒单
    @GetMapping("/reject/{id}")
    @ApiOperation(value = "拒单" )
//    @PreAuthorize(name = "拒单",value = "searchMasterMsg:reject")
    public JsonResult reject(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.reject(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"操作失败");
        }
    }

    //查询所有
    @GetMapping
    @ApiOperation(value = "查询所有数据" )
    @PreAuthorize(name = "寻主信息列表",value = "searchMasterMsg:list")
    public List<SearchMasterMsg> selectAll(){

        return searchMasterMsgService.selectAll();
    }

    //查询单个
    @GetMapping("/{id}")
    @ApiOperation(value = "查询一条数据" )
    @PreAuthorize(name = "查询寻主信息",value = "searchMasterMsg:get")
    public SearchMasterMsg selectById(@PathVariable("id") Long id){
        return searchMasterMsgService.selectById(id);
    }

    //删除单个
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除一个数据" )
    @PreAuthorize(name = "删除寻主信息",value = "searchMasterMsg:delete")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.delete(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"操作失败");
        }
    }

    //添加或修改
    @PutMapping
    @ApiOperation(value = "添加或修改数据" )
    @PreAuthorize(name = "寻主信息保存",value = "searchMasterMsg:save")
    public JsonResult updateOrAdd(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if(searchMasterMsg.getId()==null){
                searchMasterMsgService.insert(searchMasterMsg);
            }else {
                searchMasterMsgService.update(searchMasterMsg);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"操作失败");
        }
    }

    //分页和高级查询
    @PostMapping
    @ApiOperation(value = "分页查询或高级查询" )
    public PageList<SearchMasterMsg> queryPage(@RequestBody SearchMasterMsgQuery query){
        return searchMasterMsgService.selectQuery(query);
    }

    @PatchMapping
    @ApiOperation(value = "批量删除" )
    @PreAuthorize(name = "寻主信息批量删除",value = "searchMasterMsg:patchDel")
    public JsonResult delete(@RequestBody List<Long> ids){
        try {
            searchMasterMsgService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"操作失败");
        }
    }

    //寻主
    @PostMapping("/publish")
    @ApiOperation(value = "寻主发布" )
    @PreAuthorize(name = "寻主信息",value = "searchMasterMsg:publish")
    public JsonResult  publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContext.getLogininfo(request);
            searchMasterMsgService. publish(searchMasterMsg,logininfo.getId());
            return JsonResult.me();
        }catch (BusinessException e) {
            return JsonResult.me(false,e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"操作失败");
        }
    }

    /*@PostMapping("/user")
    @ApiOperation(value = "待审核" )
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        Logininfo logininfo = LoginContext.getLogininfo(request);
        return searchMasterMsgService.toAuditQuery(query,logininfo.getId());
    }*/

    @PostMapping("/tohandle")
    @ApiOperation(value = "待处理" )
    public PageList<SearchMasterMsg> tohandleQuery(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        Logininfo logininfo = LoginContext.getLogininfo(request);
        return searchMasterMsgService.tohandleQuery(query,logininfo.getId());
    }
    @PostMapping("/toAudit")
    @ApiOperation(value = "待审核" )
    public PageList<SearchMasterMsg> toAuditQuery(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        Logininfo logininfo = LoginContext.getLogininfo(request);
        return searchMasterMsgService.toAuditQuery(query,logininfo.getId());
    }

}
